package psn.kiko.controller.websocket;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import psn.kiko.pojo.R;
import psn.kiko.websocket.TextWsHandler;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * WS信息发布API
 */
@RestController
@RequestMapping("api/ws/sys")
@RequiredArgsConstructor
@Api(tags = "ws系统信息控制器")
public class WsSystemInfoController{
	private final TextWsHandler textWsHandler;
	
	@PostMapping("info")
	@ApiOperation("发布公告")
	public R<?> info(String msg) throws IOException{
		String dt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now());
		//返回html片段，样式由前端根据class添加
		String content = "<div class=\"msg\"><span class=\"txt\">"+dt+"[系统信息]:</span>"+msg+"</div>";
		textWsHandler.broadCast(content);
		return R.ok();
	}
}
